﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using ApplLogic.ApplClasses;
using TCPClient.RequestsToServerHandlers;
using TCPClient.UI.LogicForUserControls;

namespace TCPClient.UI.UserControls
{

    public enum eManagerViewType
    {
        Requests,
        Registrations,
    }

    [Designer("System.Windows.Forms.Design.ParentControlDesigner, System.Design", typeof(IDesigner))]
    public partial class ScMainManagerUserControl : UserControl
    {
        private eManagerViewType m_ManagerViewType = eManagerViewType.Registrations;
        private MoveWorkerBetweenShiftsHandler m_MoveWorkerBetweenShiftsHandler = new MoveWorkerBetweenShiftsHandler();
        private WorkersFilterHandler m_WorkersFilterHandler = null;

        public ScMainManagerUserControl()
        {
            InitializeComponent();
        }

        public RequestsToServerHandler ManagerRequestHandler
        {
            set
            {
                ScMainUserControlLogicManager.Create(value);
                setAllDefaults();
            }
        }

        private void setAllDefaults()
        {
            boundComboBoxWithAllWorkers();
            initTheWeekControlAndDays();
            setViewAs(eManagerViewType.Registrations);
            drawShiftsAccordingToTheViewState();
            registerMeToEvents();
        }

        private void boundComboBoxWithAllWorkers()
        {
            m_WorkersFilterHandler = new WorkersFilterHandler();
            comboBoxFilter.DataSource = m_WorkersFilterHandler.GetAllWorkersFullNames();
        }

        private void registerMeToEvents()
        {
            weekTableManagerUserControl1.SetCheckBoxToListenToToAllWorkers(checkBoxchangeRegistraions, checkBoxSwapWorkers);
            registerToClickEventsOfTheSingleWorkersUserControlsLabel();
            registerToClickEventsOfTheSingleWorkersUserControls();
        }

        private void registerToClickEventsOfTheSingleWorkersUserControls()
        {
            weekTableManagerUserControl1.RegisterToClickEventsOfTheSingleWorkersUserControls(
                doOnEventsOfTheSingleWorkersUserControlsClicked);
        }

        private void registerToClickEventsOfTheSingleWorkersUserControlsLabel()
        {
            weekTableManagerUserControl1.RegisterToClickEventsOfTheSingleWorkersLabelUserControls(
                doOnEventsOfTheSingleWorkersUserControlsLabelClicked);
        }

        private void doOnEventsOfTheSingleWorkersUserControlsClicked(object sender, EventArgs e)
        {
            setMoveType();
            ApplWorker worker = sender as ApplWorker;
            SingleWorkerInShiftEventArgs eventArgs = e as SingleWorkerInShiftEventArgs;
            if (eventArgs.StartTime >= ScMainUserControlLogicManager.Instance.StartTimeOfCurrentWeek.AddDays(7) &&
                eventArgs.EndTime <= ScMainUserControlLogicManager.Instance.EndTimeOfCurrentWeek.AddDays(7)
                && worker != null)
            {
                m_MoveWorkerBetweenShiftsHandler.SetWorkerAndDates(worker, eventArgs.StartTime, eventArgs.EndTime);
                if (!m_MoveWorkerBetweenShiftsHandler.IsGotWorker)
                {
                    if (!m_MoveWorkerBetweenShiftsHandler.FinishMessage.Equals("Ok"))
                    {
                        MessageBox.Show(m_MoveWorkerBetweenShiftsHandler.FinishMessage, @"Re-registrarion failed",
                                MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    drawSpecificShifsThatChanged();
                }
            }
        }

        private void setMoveType()
        {
            m_MoveWorkerBetweenShiftsHandler.MoveType = checkBoxSwapWorkers.Checked ? eMoveType.Swap : eMoveType.Move;
        }

        private void drawSpecificShifsThatChanged()
        {
            foreach (var kvp in ScMainUserControlLogicManager.Instance.GetResultsOfSpecificShift(
                m_MoveWorkerBetweenShiftsHandler.StartTimeOfSourceShift, m_MoveWorkerBetweenShiftsHandler.EndTimeOfSourceShift))
            {
                weekTableManagerUserControl1.DrawRegisteredWorkers(kvp.Key.BeginTime, kvp.Key.EndTime, kvp.Value, true);
            }
            foreach (var kvp in ScMainUserControlLogicManager.Instance.GetResultsOfSpecificShift(
                  m_MoveWorkerBetweenShiftsHandler.StartTimeOfDestShift, m_MoveWorkerBetweenShiftsHandler.EndTimeOfDestShift))
            {
                weekTableManagerUserControl1.DrawRegisteredWorkers(kvp.Key.BeginTime, kvp.Key.EndTime, kvp.Value, true);
            }
        }

        private void doOnEventsOfTheSingleWorkersUserControlsLabelClicked(object sender, EventArgs e)
        {
            ApplWorker worker = (sender as SingleWorkerInShiftUserControl).Worker;
            PersonalDetailsOfWorkerForManager pform = new PersonalDetailsOfWorkerForManager(worker);
            pform.ShowWorkerRequestedAndREgistered();
            pform.ShowDialog();
        }

        private void drawShiftsAccordingToTheViewState()
        {
            switch (m_ManagerViewType)
            {
                case eManagerViewType.Registrations:
                    drawRegisteredWorkers();
                    break;
                case eManagerViewType.Requests:
                    drawRequestedWorkers();
                    break;
            }
        }

        private bool isDrawAllWorkers()
        {
            return comboBoxFilter.SelectedValue as string == "All Workers";
        }

        private void drawRequestedWorkers()
        {
            weekTableManagerUserControl1.CleanAll();
            if (isDrawAllWorkers())
            {
                var ddd = ScMainUserControlLogicManager.Instance.GetAllRequestsOfAllShifts();
                foreach (var kvp in ddd)
                {
                    weekTableManagerUserControl1.DrawRequestedWorkers(kvp.Key.BeginTime, kvp.Key.EndTime, kvp.Value);
                }
            }
            else
            {
                foreach (var kvp in ScMainUserControlLogicManager.Instance.GetAllRequestsOfAllShiftsOfWorker
                    (m_WorkersFilterHandler.GetIdByName(comboBoxFilter.SelectedValue as string)))
                {
                    weekTableManagerUserControl1.DrawRequestedWorkers(kvp.Key.BeginTime, kvp.Key.EndTime, kvp.Value);
                }
            }
        }

        private void drawRegisteredWorkers()
        {
            weekTableManagerUserControl1.CleanAll();
            if (isDrawAllWorkers())
            {
                foreach (var kvp in ScMainUserControlLogicManager.Instance.GetAllResultsfAllShifts())
                {
                    weekTableManagerUserControl1.DrawRegisteredWorkers(kvp.Key.BeginTime, kvp.Key.EndTime, kvp.Value);
                }
            }
            else
            {
                foreach (var kvp in ScMainUserControlLogicManager.Instance.GetAllResultsOfAllShiftsOfWorker
                    (m_WorkersFilterHandler.GetIdByName(comboBoxFilter.SelectedValue as string)))
                {
                    weekTableManagerUserControl1.DrawRegisteredWorkers(kvp.Key.BeginTime, kvp.Key.EndTime, kvp.Value);
                }
            }
        }

        private void setViewAs(eManagerViewType viewType)
        {
            setTheCorrectIndex(viewType);
        }

        private void setTheCorrectIndex(eManagerViewType viewType)
        {
            switch (viewType)
            {
                case eManagerViewType.Registrations:
                    this.requestsResultsIndexUserControl1.Visible = true;
                    this.requestIndexUserControl1.Visible = false;
                    setChangingTheWorkersRegistrationsConfigurations();
                    break;
                case eManagerViewType.Requests:
                    this.requestsResultsIndexUserControl1.Visible = false;
                    this.requestIndexUserControl1.Visible = true;
                    setChangingTheWorkersRegistrationsNotVisible();
                    break;
            }
        }

        private void setChangingTheWorkersRegistrationsNotVisible()
        {
            panel1.Visible = false;
            labelChangeRegistraions.Visible = false;
            checkBoxchangeRegistraions.Visible = false;
            labelSwapRegistraions.Visible = false;
            checkBoxSwapWorkers.Visible = false;
        }

        private void setChangingTheWorkersRegistrationsConfigurations()
        {
            panel1.Visible = true;
            checkBoxchangeRegistraions.Visible = true;
            checkBoxSwapWorkers.Visible = true;
            labelSwapRegistraions.Visible = true;
            labelChangeRegistraions.Visible = true;
            if (weekTableManagerUserControl1.StartOfWeek.DayOfYear ==
                    ScMainUserControlLogicManager.Instance.StartTimeOfCurrentWeek.AddDays(7).DayOfYear && isDrawAllWorkers())
            {
                panel1.Enabled = true;
                checkBoxchangeRegistraions.Enabled = true;
                checkBoxchangeRegistraions.Checked = false;
                labelChangeRegistraions.Enabled = true;
                labelSwapRegistraions.Enabled = true;
                checkBoxSwapWorkers.Enabled = true;
                checkBoxSwapWorkers.Checked = false;
            }
            else
            {
                panel1.Enabled = false;
                checkBoxchangeRegistraions.Enabled = false;
                checkBoxchangeRegistraions.Checked = false;
                labelChangeRegistraions.Enabled = false;
                labelSwapRegistraions.Enabled = false;
                checkBoxSwapWorkers.Enabled = false;
                checkBoxSwapWorkers.Checked = false;
            }
        }

        private void initTheWeekControlAndDays()
        {
            weekTableManagerUserControl1.SetWeekData(ScMainUserControlLogicManager.Instance.StartTimeOfTheWeek,
                                                     ScMainUserControlLogicManager.Instance.EndTimeOfTheWeek);

        }

        private void tabControlRegisterdAndRequired_Selected(object sender, TabControlEventArgs e)
        {
            setAllShiftsDataAccordingtoTab((e as TabControlEventArgs).TabPage.Text);
        }

        private void setAllShiftsDataAccordingtoTab(string dataTab)
        {
            if (dataTab == eManagerViewType.Registrations.ToString())
            {
                weekTableManagerUserControl1.SetWeekData(ScMainUserControlLogicManager.Instance.StartTimeOfTheWeek,
                                 ScMainUserControlLogicManager.Instance.EndTimeOfTheWeek);
                drawRegisteredWorkers();
                setViewAs(eManagerViewType.Registrations);

                

            }
            else if (dataTab == eManagerViewType.Requests.ToString())
            {
                weekTableManagerUserControl1.SetWeekData(ScMainUserControlLogicManager.Instance.StartTimeOfTheWeek,
                  ScMainUserControlLogicManager.Instance.EndTimeOfTheWeek);
                drawRequestedWorkers();
                setViewAs(eManagerViewType.Requests);
            }
        }

        private void monthCalendar1_DateChanged(object sender, DateRangeEventArgs e)
        {
            ScMainUserControlLogicManager.Instance.CaclulateStartAndEndTimeOfTheWeek(e.Start);
            if (weekTableManagerUserControl1.StartOfWeek.DayOfYear != ScMainUserControlLogicManager.Instance.StartTimeOfTheWeek.DayOfYear)
            {
                setAllShiftsDataAccordingtoTab(this.tabControlRegisterdAndRequired.SelectedTab.Text);
            }
        }

        public void RefreshAll()
        {
            setAllShiftsDataAccordingtoTab(tabControlRegisterdAndRequired.SelectedTab.Text);
        }

        public void OperateAlgo()
        {
            AlgoExecuterForm algoExecuterForm = new AlgoExecuterForm();
            algoExecuterForm.ShowDialog();
        }

        public void PublishWorkOrder()
        {
            PublishWorkOrderForm pfm = new PublishWorkOrderForm(eNotificationType.NotifyForNewWorkOrder);
            pfm.ShowDialog();
        }

        private void checkBoxchangeRegistraions_CheckStateChanged(object sender, EventArgs e)
        {
            if((sender as CheckBox).CheckState == CheckState.Checked)
            {
                checkBoxSwapWorkers.Checked = false;
            }
        }

        private void checkBoxSwapWorkers_CheckStateChanged(object sender, EventArgs e)
        {
            if ((sender as CheckBox).CheckState == CheckState.Checked)
            {
                checkBoxchangeRegistraions.Checked = false;
            }
        }

        private void comboBoxFilter_SelectedIndexChanged(object sender, EventArgs e)
        {
            ComboBox comboBox = sender as ComboBox;
            setAllShiftsDataAccordingtoTab(tabControlRegisterdAndRequired.SelectedTab.Text);
        }
    }
}
